﻿Imports System.Data.SqlClient
Public Class frmLstLocation
    Dim cond As String = ""
    Dim frmLoad As Boolean = False

    Public Sub lstLocation(ByVal condition As String)
        lstVDemandes.Items.Clear()
        Cmd = New SqlCommand("select tl.numLocation,Nom_entreprise + c.Nom + ' ' + c.Prenom as fullName,tl.dateLivraison,tl.adresse,p.Nom + ' ' + p.Prenom as livreur,tl.dateRecuperation from Clients c " &
                            "inner join locEvent le on le.id = c.Id_Client " &
                            "inner join trLocation tl on tl.idLocEvent = le.id " &
                            "inner join Personnels p on p.Id_Personnels = tl.chargeLivraison " &
                            condition, Con)
        If condition.Contains("tl.dateLivraison") Then
            dteLoc = New System.DateTime(dtpLivraison.Value.Year, dtpLivraison.Value.Month, dtpLivraison.Value.Day)
            Cmd.Parameters.AddWithValue("@dteLiv", dteLoc)
        End If
        Dr = Cmd.ExecuteReader
        While Dr.Read
            lstVDemandes.Items.Add(New ListViewItem(New String() {Dr.GetInt32(0), Dr.GetString(1), String.Format("{0:d/M/yyyy HH:mm}", Dr.GetDateTime(2)), String.Format("{0:d/M/yyyy HH:mm}", Dr.GetDateTime(5)), Dr.GetString(4), Dr.GetString(3)}))
        End While
        Dr.Close()
        cond = ""
        txtNumBon.Text = ""
    End Sub

    Private Sub frmLstLocation_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        cnClose()
    End Sub


    Private Sub frmLstLocation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = Frm_Main
        open()
        lstLocation("")
        frmLoad = True
    End Sub


    Private Sub chkBoxtsMoment_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBoxtsMoment.CheckedChanged
        If frmLoad Then
            If chkBoxtsMoment.Checked Then
                dtpLivraison.Enabled = False
                cond = ""
            Else
                dtpLivraison.Enabled = True
                cond = "where convert(varchar,tl.dateLivraison,111) = @dteLiv"
            End If
            lstLocation(cond)
        End If
    End Sub

    Private Sub dtpLivraison_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpLivraison.ValueChanged
        If Not chkBoxtsMoment.Checked Then
            cond = "where convert(varchar,tl.dateLivraison,111) = @dteLiv"
        lstLocation(cond)
        End If
    End Sub

    Private Sub lstDemandes_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstVDemandes.DoubleClick
        If lstVDemandes.SelectedItems.Count > 0 Then
            numLocation = CInt(lstVDemandes.SelectedItems(0).SubItems(0).Text)
            Me.Close()
            Frm_Demande_Clients_loc.Show()
        End If
    End Sub

    Private Sub lstVDemandes_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstVDemandes.SelectedIndexChanged
        If lstVDemandes.SelectedItems.Count > 0 Then
            numLocation = CInt(lstVDemandes.SelectedItems(0).SubItems(0).Text)
        End If
    End Sub

    Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRecover.Click
        If lstVDemandes.SelectedItems.Count > 0 Then
            'Dim dte As New DateTime
            'dte = CDate(lstVDemandes.SelectedItems(0).SubItems(3).Text)
            'dte = dte.AddHours(5)
            'If Now <= dte Then
            '    MsgBox("Vous ne pouvez pas récupérer les équipements maintenant!!!" & Environment.NewLine & "La récupération est possible après 5 heures de la livraison au minimum", MsgBoxStyle.Critical, "Erreur")
            '    Exit Sub
            'End If
            If lstVDemandes.SelectedItems.Count > 0 Then
                If Date.Now >= CDate(lstVDemandes.SelectedItems(0).SubItems(3).Text) Then
                    Me.Close()
                    frmRecoverEquips.Show()
                Else
                    MsgBox("Vous ne pouvez pas récupérer les équipements maintenant!!!" & Environment.NewLine & "La récupération des équipements et prévu le " & CDate(lstVDemandes.SelectedItems(0).SubItems(3).Text).ToShortDateString _
                           & " à " & CDate(lstVDemandes.SelectedItems(0).SubItems(3).Text).ToShortTimeString, MsgBoxStyle.Critical, "Erreur")
                End If
            End If
        Else
            MsgBox("Vous devez séléctionner une location!!!", MsgBoxStyle.Critical, "Erreur")
        End If
    End Sub
    Dim arr As New List(Of ListViewItem)
    Private Sub txtNumBon_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNumBon.TextChanged
        For Each itArr In arr
            lstVDemandes.Items.Add(itArr)
        Next
        arr.Clear()
        For Each it As ListViewItem In lstVDemandes.Items
            If it.SubItems(0).Text <> txtNumBon.Text And txtNumBon.Text <> "" Then
                arr.Add(it)
                it.Remove()
            End If
        Next

    End Sub
End Class